package com.qianchang.microfinance.view.mine.renewal;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.dm.library.utils.FormatUtil;
import com.dm.library.utils.ViewUtils;
import com.dm.library.widgets.element.DTextView;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.LoanProductBean;
import com.qianchang.microfinance.bean.RechargeListItemBean;
import com.qianchang.microfinance.bean.SubmitLoanBean;
import com.qianchang.microfinance.global.ConstantAppInfo;
import com.qianchang.microfinance.view.base.BaseRecyclerAdapter;
import com.qianchang.microfinance.view.mine.loan.ConfirmAgreementActivity;

import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;

public class RenewalAdapter extends BaseRecyclerAdapter<LoanProductBean> implements View.OnClickListener{

    private Context context;
    private LayoutInflater inflater;
    private List<LoanProductBean> list = new ArrayList<LoanProductBean>();
    private RenewalAdapter.OnItemClickListener mOnItemClickListener = null;

    public RenewalAdapter(Context mContext) {
        super(mContext, BaseRecyclerAdapter.ONLY_FOOTER);
        context = mContext;
    }


//    public RenewalAdapter(Context mContext){
//        this.context = mContext;
//        inflater = LayoutInflater.from(context);
//    }

    public void setData(List<LoanProductBean> mlist){
        if(mlist != null){
            list = mlist;
        }
        notifyDataSetChanged();
    }

//    public void clear(){
//        if(list != null){
//            list.clear();
//        }
//        notifyDataSetChanged();
//    }


//    public void addAll(List<LoanProductBean> items) {
//        if (items != null) {
//            this.list.addAll(items);
//            notifyItemRangeInserted(this.list.size(), items.size());
//        }
//    }


//    @Override
//    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        View view = LayoutInflater.from(context).inflate(R.layout.adapter_renewal, parent, false);
//        return new RenewalViewHolder(view,mOnItemClickListener);
//    }
//
//
//
//
//    @Override
//    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//        RenewalAdapter.RenewalViewHolder myLoanViewHolder = (RenewalAdapter.RenewalViewHolder) holder;
//
//
//        LoanProductBean item = list.get(position);
//
//        myLoanViewHolder.tvPeriod.setTextContent(item.getDeadline());
//        String createTime = item.getCreateTime();
//        myLoanViewHolder.tvApplyDate.setTextContent(ViewUtils.formatDateToDay(createTime));
//        String status = item.getStatus();
//        myLoanViewHolder.tvStatus.setTextContent(item.getStatusDesc());
//        double loanAmount = item.getLoanAmount();
//
//        if (loanAmount == 0) {
//            myLoanViewHolder.tvMoney.setTextContent(FormatUtil.numKbPointFormat(item.getApplyAmount()));
//        } else {
//            myLoanViewHolder.tvMoney.setTextContent(FormatUtil.numKbPointFormat(loanAmount));
//        }
//
//        if ("4".equals(status)) {
//            myLoanViewHolder.llStatus.setVisibility(View.GONE);
//            myLoanViewHolder.tvSureAgreement.setVisibility(View.VISIBLE);
//        } else {
//            myLoanViewHolder.llStatus.setVisibility(View.VISIBLE);
//            myLoanViewHolder.tvSureAgreement.setVisibility(View.GONE);
//        }
//        myLoanViewHolder.tvSureAgreement.setOnClickListener(this);
//        myLoanViewHolder.tvSureAgreement.setTag(position);
//    }
//
//
//    @Override
//    public int getItemCount() {
//        return list.size();
//    }

    @Override
    protected RecyclerView.ViewHolder onCreateDefaultViewHolder(ViewGroup parent, int type) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_renewal, parent, false);
        return new RenewalViewHolder(view,mOnItemClickListener);
    }

    @Override
    protected void onBindDefaultViewHolder(RecyclerView.ViewHolder holder, LoanProductBean item, int position) {
        RenewalAdapter.RenewalViewHolder myLoanViewHolder = (RenewalAdapter.RenewalViewHolder) holder;


//        LoanProductBean item = list.get(position);

        myLoanViewHolder.tvPeriod.setTextContent(item.getDeadline());
        String createTime = item.getCreateTime();
        myLoanViewHolder.tvApplyDate.setTextContent(ViewUtils.formatDateToDay(createTime));
        String status = item.getStatus();
        myLoanViewHolder.tvStatus.setTextContent(item.getStatusDesc());
        double loanAmount = item.getLoanAmount();

        if (loanAmount == 0) {
            myLoanViewHolder.tvMoney.setTextContent(FormatUtil.numKbPointFormat(item.getApplyAmount()));
        } else {
            myLoanViewHolder.tvMoney.setTextContent(FormatUtil.numKbPointFormat(loanAmount));
        }

        if ("4".equals(status)) {
            myLoanViewHolder.llStatus.setVisibility(View.GONE);
            myLoanViewHolder.tvSureAgreement.setVisibility(View.VISIBLE);
        } else {
            myLoanViewHolder.llStatus.setVisibility(View.VISIBLE);
            myLoanViewHolder.tvSureAgreement.setVisibility(View.GONE);
        }
        myLoanViewHolder.tvSureAgreement.setOnClickListener(this);
        myLoanViewHolder.tvSureAgreement.setTag(position);
    }

    @Override
    public void onClick(View view) {
        int position = (int) view.getTag();
        Intent intent = new Intent(context, ConfirmAgreementActivity.class);
        Bundle bundle = new Bundle();
        LoanProductBean loanProductBean = list.get(position);
        bundle.putString(ConstantAppInfo.PROJECT_ID, loanProductBean.getProjectId());
        bundle.putInt(ConstantAppInfo.PROJECT_TYPE, loanProductBean.getProductType());
        bundle.putString(SubmitLoanBean.STATUS, loanProductBean.getStatusDesc());
        bundle.putString(SubmitLoanBean.LOAN_AMOUNT, loanProductBean.getApplyAmount());
        bundle.putInt(ConstantAppInfo.FROM, 1);
        intent.putExtras(bundle);
        context.startActivity(intent, bundle);
    }

    static class RenewalViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        @BindView(R.id.tv_money)
        DTextView tvMoney;
        @BindView(R.id.tv_period)
        DTextView tvPeriod;
        @BindView(R.id.tv_apply_title)
        DTextView tvApplyTitle;
        @BindView(R.id.tv_apply_date)
        DTextView tvApplyDate;
        @BindView(R.id.tv_status)
        DTextView tvStatus;
        @BindView(R.id.tv_sure_agreement)
        DTextView tvSureAgreement;
        @BindView(R.id.ll_status)
        LinearLayout llStatus;

        private OnItemClickListener onItemClickListener;


        public RenewalViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public RenewalViewHolder(View itemView,OnItemClickListener mOnItemClickListener) {
            super(itemView);
            onItemClickListener = mOnItemClickListener;
            itemView.setOnClickListener(this);
            ButterKnife.bind(this, itemView);
        }

        @Override
        public void onClick(View view) {
            if(onItemClickListener != null){
                onItemClickListener.onItemClick(view,getPosition());
            }
        }
    }


    public interface OnItemClickListener {
        void onItemClick(View view , int position);
    }


    public void setOnItemClickListener(RenewalAdapter.OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


}
